package silencify;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScreenScraper {

	/**
	 * @param args
	 * @throws AWTException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws AWTException, IOException, InterruptedException {
		BufferedImage searchImage = ImageIO.read(new File("spotify-region.png"));
		Robot robot = new Robot();
//		Robot robot = new Robot(getGraphicDevices()[1]);
		Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
		File target = new File("screen.png");
		BufferedImage screenImg = robot.createScreenCapture(area); 
		saveImageToFile(screenImg, target);
		
		Point p = findRegion(searchImage, screenImg);
		System.out.printf("%s%n", p);
		if (p != null) {
			int x = p.x-53;
			int y = p.y+19;
			robot.mouseMove(x, y);
			Color c1 = robot.getPixelColor(x, y);
			Color c2 = robot.getPixelColor(x+10, y);
			System.out.printf("Same colors:%s%n",c1.equals(c2));
			Thread.sleep(1000);
			robot.mouseMove(x+10, y);
		}

//		test();
	}

	private static void saveImageToFile(BufferedImage image,
			File file) throws IOException {
		ImageIO.write(image, "png", file);
	}
	
	public static GraphicsDevice[] getGraphicDevices()
	{
		   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		   GraphicsDevice[] gs = ge.getScreenDevices();
		   return gs;
	}

	public static Pair<Point, Robot> findRegion(BufferedImage image) throws AWTException {
		for (GraphicsDevice d : getGraphicDevices()) {
			Robot r = new Robot(d);
			Point p = findRegion(image, r);
			if (p != null) {
				return new Pair<Point, Robot>(p, r);
			}
		}
		return null;
	}
	public static Point findRegion(BufferedImage image, Robot robot) {
		Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
		BufferedImage screenImg = robot.createScreenCapture(area);
		return findRegion(image, screenImg);
	}

	static public Point findRegion(BufferedImage image, BufferedImage screenImg) {
		int w = image.getWidth();
		int sw = screenImg.getWidth();
		int h = image.getHeight();
		int sh = screenImg.getHeight();
		for (int x=0 ; x<sw-w ; x++) {
			for (int y=0 ; y<sh-h ; y++) {
				if (equals(image, screenImg, x,y))
					return new Point(x, y);
			}
		}
		return null;
	}
	private static boolean equals(BufferedImage image, BufferedImage screenImg, int x, int y) {
		int w = image.getWidth();
		int h = image.getHeight();
		for (int xx=0 ; xx<w ; xx++) {
			for (int yy=0 ; yy<h ; yy++) {
				if (image.getRGB(xx, yy) != screenImg.getRGB(x+xx, y+yy)) 
					return false;
			}
		}
		return true;
	}
}
